package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.ClassesCoursePlanQuery;
import com.matt.foundation.vo.course.VendorCoursePrepareDetailVO;
import com.matt.foundation.vo.course.WebCoursePlanListVo;
import com.matt.service.CourseAuditService;
import com.matt.service.PrepareCourseService;
import com.matt.service.TeacherService;
import com.matt.service.remoteCall.DTO.RoleDTO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/11
 * *******************************
 */
@RestController
@RequestMapping("/web/teacher")
@Api(tags = "班主任_教师")
public class WebTeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private PrepareCourseService prepareCourseService;

    @Autowired
    private CourseAuditService courseAuditService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private MemVerificationCache cache;

    @ApiOperation(value = "新的 WEB 教师详情， 以降低对eh项目的权限接口依赖",httpMethod = "POST")
    @RequestMapping("/info_v2")
    public StatusResponse teacherInfoV2(@RequestHeader(RequestConstant.TICKET) String ticket,@RequestHeader(value = RequestConstant.PLATFORM_ID, required = false)String platformId) {
        Map response = ssoCallService.getPermissionInfoV2(ticket,platformId);
        return StatusResponse.success(response);
    }

    @ApiOperation(value = "我的课表",httpMethod = "POST")
    @RequestMapping("/course_list")
    public StatusResponse courseList(@RequestHeader(RequestConstant.TICKET) String ticket, ClassesCoursePlanQuery query, Integer type) {
        StaffDTO baseInfo = cache.getStaff(ticket);
        query.setStaffId(baseInfo.getId());
        Page<WebCoursePlanListVo> coursePlanList;

        if (type == 1)
            coursePlanList = prepareCourseService.getCourseList(query, false);
        else
            coursePlanList = prepareCourseService.getWebCoursePlanHistory(query, false);
        return StatusResponse.success(coursePlanList);
    }

    @ApiOperation(value = "课程计划的备课详情",httpMethod = "POST")
    @RequestMapping("/course_detail")
    public StatusResponse courseDetail(@RequestHeader(TICKET) String ticket, String coursePlanClassesId) {
        String areaId = cache.getAreaId(ticket);
        Map map = prepareCourseService.courseDetail(areaId, coursePlanClassesId);
        return StatusResponse.success(map);
    }

    @ApiOperation(value = "老师备厂商课程",httpMethod = "POST")
    @RequestMapping("/prepare_vendor_course")
    public StatusResponse prepareKXThemeCourse(@RequestHeader(TICKET) String ticket, VendorCoursePrepareDetailVO prepareDetail, String coursePlanClassesId, Integer action) {
        StaffDTO dto = cache.getStaff(ticket);
        Boolean result = prepareCourseService.prepareCourse(dto, prepareDetail, coursePlanClassesId, action);
        return StatusResponse.success(result);
    }
    
    @ApiOperation(value = "老师备园区自建课",httpMethod = "POST")
    @RequestMapping("/prepare_area_build_course")
    public StatusResponse prepareAreaBuildCourse(@RequestHeader(TICKET) String ticket,VendorCoursePrepareDetailVO prepareDetail, String coursePlanClassesId, String content, Integer action) {
        StaffDTO dto = cache.getStaff(ticket);
        Boolean result = prepareCourseService.prepareAreaBuildCourse(dto,prepareDetail ,coursePlanClassesId, content, action, null);
        return StatusResponse.success(result);
    }

    @ApiOperation(value = "未备课课程数量",httpMethod = "POST")
    @RequestMapping("/non_prepare_count")
    public StatusResponse nonPrepareCount(@RequestHeader(TICKET) String ticket, String areaId) {
        String staffId = cache.getStaff(ticket).getId();
        Long count = prepareCourseService.nonPrepareCount(staffId, areaId);
        return StatusResponse.success(count);
    }

    //设置默认岗位
    @ApiOperation(value = "设置默认岗位",httpMethod = "POST")
    @RequestMapping("/set_default_role")
    public StatusResponse setDefaultStaffPosition(@RequestHeader(TICKET) String ticket,String roleId){
//        String staffId = cache.getStaffId(ticket);
        RoleDTO result = ssoCallService.setDefaultRole(ticket, roleId);
        return StatusResponse.success(result);
    }

}
